/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Connection;

/**
 *
 * @author Bruno Lopes
 */
import Exceptions.DataAccessException;
import com.mysql.jdbc.PreparedStatement;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Connect {

    private static Connection conn;
    private static Properties propriedades;

    public static Connection createConnection() throws ClassNotFoundException,
            SQLException,
            FileNotFoundException,
            IOException,
            DataAccessException {

        propriedades = new Properties();
        FileInputStream in = new FileInputStream("src/"
                + "Dao/"
                + "DadosAcessoBd.properties");

        propriedades.load(in);
        in.close();

        String driver = propriedades.getProperty("Driver");
        if (driver != null) {
            Class.forName(driver);
        } else {
            throw new DataAccessException("Erro no acesso à base de dados");
        }

        String url = propriedades.getProperty("Url");
        String username = propriedades.getProperty("Username");
        String password = propriedades.getProperty("Password");

        conn = DriverManager.getConnection(url, username, password);

        return conn;
    }

    public static void closeConnection(java.sql.PreparedStatement pstmt) throws SQLException {
        pstmt.close();
        conn.close();
    }
}
